<template>
    <div style="width: 1000px;">
        <div class="flex_b">
            <div>
                <el-button type="primary" @click="addBtn" size="mini">添加用户</el-button>
            </div>
        </div>
		<br>
        <el-table  :data="result"   border  style="width: 100%" >
            <el-table-column   align="center"    prop="nick_name"    label="名称"></el-table-column>
            <el-table-column  align="center"   prop="uname"  label="登录名"></el-table-column>
			<el-table-column  label="密码"  align="center">
			    <template slot-scope="scope">
			        <el-input  type="password"
						size="mini"
						v-model="scope.row.upwd" @blur="updateUpwd(scope.row.uid,scope.row.upwd)" />
			    </template>
			</el-table-column>
            <el-table-column    align="center"  label="岗位">
				<template slot-scope="scope">
				    <!-- {{jobType.filter(item)=>{ item.id==scope.row.job}}} -->
					{{jobType.filter((item)=>{ return item.id==scope.row.job })[0].post_name}}
				</template>
			</el-table-column>
			<el-table-column  label="状态"  align="center">
				<template slot-scope="scope">
				   <el-switch
				     v-model="scope.row.mark"
					 :inactive-value="'0'"
					 :active-value="'1'"
					 @change="changeData(scope.row)"
					 >
				   </el-switch>
				</template>
			</el-table-column>
            <el-table-column  label="操作"  fixed="right"  align="center">
                <template slot-scope="scope">
                    <el-button @click="updateData(scope.row)" type="text" size="small">更新</el-button>
                    <el-button type="text" size="small" @click="delData(scope.row.uid,scope)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        
		
		<el-dialog
		        :title="title"
				:close-on-click-modal="false"
		        :visible.sync="isShow"
		        width="700px"
		>
		    <el-form  :model="info" label-width="160px">
		        <el-form-item label="名称">
		            <el-input size="small" v-model="info.nick_name" style="width: 500px"></el-input>
		        </el-form-item>
		    <el-form-item label="登录名">
		        <el-input size="small" v-model="info.uname" style="width: 500px"></el-input>
		    </el-form-item>
		    <el-form-item label="密码" v-if="title=='添加用户'">
		        <el-input size="small" type="password"  v-model="info.upwd" style="width: 500px"></el-input>
		    </el-form-item>
			<el-form-item label="岗位">
			    <el-select v-model="info.job"   size="small">
			        <el-option
			          v-for="item in jobType"
			          :key="item.id"
			          :label="item.post_name"
			          :value="item.id">
			        </el-option>
			      </el-select>
			</el-form-item>
			<el-form-item label="状态">
			    <el-radio-group v-model="info.mark">
			       <el-radio label="0">不启用</el-radio>
			        <el-radio label="1">启用</el-radio>
			     </el-radio-group>
			</el-form-item>
			<el-form-item label="是否可以删除单据">
			    <el-radio-group v-model="info.isDeleteOrder">
			       <el-radio label="0">不可以</el-radio>
			        <el-radio label="1">可以</el-radio>
			     </el-radio-group>
			</el-form-item>
			<el-form-item label="是否可以修改单据日期">
			    <el-radio-group v-model="info.isUpdateOrderDate">
			       <el-radio label="0">不可以</el-radio>
			        <el-radio label="1">可以</el-radio>
			     </el-radio-group>
			</el-form-item>
		    </el-form>
		    <span slot="footer" class="dialog-footer">
		            <el-button @click="isShow = false" size="mini">取 消</el-button>
		            <el-button :loading="loading_btn" type="primary" size="mini" @click="confirm">
						确 定
					</el-button>
		        </span>
		</el-dialog>
       
    </div>
</template>
<style>

</style>
<script>
    export default {
        data(){
            return {
                result:[],
				isShow:false,
				title:'添加用户',
				info:{
					uid:undefined,
					nick_name:"", //名称
					uname:"", //登录名
					upwd:"",//密码
					job:"",//岗位
					mark:'1',//启用标志
					isDeleteOrder:'0',
					isUpdateOrderDate:'0'
				},
				jobType:[]
            }
        },
        mounted(){
			this.lodeList()         
			this.lodejob()
            document.title='用户管理'
			
        },
        methods:{
			lodejob(){
			    this.$post('/system/postJobList',{status:1},res=>{
			    	 let result = res.data
			    	if(result.code == 200){
						// this.result = result.data
						this.jobType = result.data
			    	}
			    })
			},
			changeData(row){
				for(let i in this.info){
					this.info[i] = row[i]
				}
				this.confirm()
			},
			delData(uid,scope){
				let index = scope.$index
				this.$confirm('确定删除该用户', '提示', {
				  confirmButtonText: '确定',
				  cancelButtonText: '取消',
				  type: 'warning'
				}).then(() => {
					this.$post('/system/delUser',{uid},res=>{
						let result = res.data
						if(result.code == 200){
							this.$message.success('删除成功')
							this.result.splice(index,1)
							
						}
					})
				})
			},
			updateData(row){
				for(let i in this.info){
					this.info[i] = row[i]
				}
				this.isShow = true
				this.title = '修改用户'
			},
			updateUpwd(uid,upwd){
				if(upwd.indexOf('*')!=-1 || !upwd) return
				this.$confirm('确定修改密码', '提示', {
				  confirmButtonText: '确定',
				  cancelButtonText: '取消',
				  type: 'warning'
				}).then(() => {
					this.$post('/system/editUserUpwd',{uid,upwd},res=>{
						let result = res.data
						if(result.code == 200){
							this.$message.success('更新成功')
						}
					})
				})
			},
			confirm(){
				this.loading_btn = true
				this.$post('/system/addEditUser',this.info,res=>{
					 let result = res.data
					if(result.code == 200){
						this.$message.success(result.msg)
						this.isShow = false
						this.lodeList()
					}else{
						this.$message.error(result.msg)
					}
					this.loading_btn = false
				})
			},
            addBtn(){
				this.isShow = true
				this.title = '添加用户'
				this.info = {
					uid:undefined,
					nick_name:"", //名称
					uname:"", //登录名
					upwd:"",//密码
					job:"",//岗位
					mark:'1',//启用标志
					isDeleteOrder:'0',
					isUpdateOrderDate:'0'
				}
			},
            lodeList(){
                this.$post('/system/userList',this.info,res=>{
                	 let result = res.data
                	if(result.code == 200){
						for(let item of result.data){
							item.upwd = '******'
						}
						this.result = result.data
                	}
                })
            },
            
        }
    }
</script>
